package com.xbongbong.crm.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.abstracts.AbstractBusinessLinkProductService;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.entity.QuotationProductEntity;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.QuotationEnum;
import com.xbongbong.saas.model.QuotationModel;
import com.xbongbong.saas.model.QuotationProductModel;
import com.xbongbong.util.SaasLinkProductUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 吴峰
 * @date 2022/06/10 14:23
 */
@Slf4j
@Service("quotationLinkProductServiceImpl")
public class QuotationLinkProductServiceImpl extends AbstractBusinessLinkProductService {

    @Resource
    private QuotationProductModel quotationProductModel;
    @Resource
    private QuotationModel quotationModel;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.QUOTATION.getCode());
    }

    @Override
    public void analysisListProductData(FormDataListDTO formDataListDTO, List<PaasFormDataEntityExt> paasFormDataESList, List<FieldAttrEntity> explainList, Integer page, Integer pageSize) throws XbbException {
        if (CollectionUtils.isEmpty(paasFormDataESList)) {
            return;
        }
        String corpid = formDataListDTO.getCorpid();
        boolean productNameNeedHide = getProductNameVisible(formDataListDTO.getLoginUser(), corpid);

        Map<String, FieldAttrEntity> explainMap = explainList.stream().collect(Collectors.toMap(FieldAttrEntity:: getAttr, attr-> attr));
        if (!explainMap.containsKey(QuotationEnum.PRODUCT.getAttr())){
            return ;
        }
        FieldAttrEntity fieldAttrEntity = explainMap.get(QuotationEnum.PRODUCT.getAttr());
        List<FieldAttrEntity> linkProductList = SaasLinkProductUtil.listProductLinkKeyField(fieldAttrEntity);


        explainMap.put(QuotationEnum.PRODUCT.getAttr(), fieldAttrEntity);
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("del",0);
        map.put("startGroup",page);
        map.put("pageNumGroup",pageSize);
        if (Objects.isNull(pageSize)){
            map.put("orderByStrGroup", BasicConstant.SORT);
        } else {
            map.put("orderByStr", BasicConstant.SORT);
        }
        List<Long> dataIdList = new ArrayList<>();
        paasFormDataESList.forEach(item->dataIdList.add(item.getDataId()));
        map.put("quotationIdIn",dataIdList);
        if (Objects.isNull(pageSize)) {
            map.put("groupByStr", "quotation_id");
            map.put(ParameterConstant.ORDER_BY_STR_GROUP, BasicConstant.SORT);
        }else {
            map.put(ParameterConstant.ORDER_BY_STR, BasicConstant.SORT);
        }
        List<QuotationProductEntity> list = quotationProductModel.findEntitys(map);
        Map<Long, List<JSONObject>> productDataMap = new HashMap<>(paasFormDataESList.size());
        Set<Long> productIdSet = new HashSet<>();
        for (QuotationProductEntity item : list) {
            Long quotationId = item.getQuotationId();
            JSONObject productData = quotationModel.formatSingleProduct(item);
            productData.put(StringConstant.DATA_ID, productData.getLong(SelectProductEnum.PRODUCT.getAttr()));
            productData.put(SelectProductEnum.PRODUCT.getAttr(), productNameNeedHide ? null : item.getProductName());
            List<JSONObject> productDataList = productDataMap.getOrDefault(quotationId, new ArrayList<>());
            productDataList.add(productData);
            // 以机会id为key，关联产品为value的数据集合
            productDataMap.put(quotationId, productDataList);
            // 产品id容器
            productIdSet.add(item.getProductId());
        }
        // 将数据放到关联产品中
        setLinkProduct(corpid, paasFormDataESList, linkProductList, productIdSet, productDataMap, QuotationEnum.PRODUCT.getAttr());
    }
}
